package simple; /**
 * @projectName study-note
 * @package PACKAGE_NAME
 * @className PACKAGE_NAME.DeleteDuplicates
 */

import node.ListNode;

/**
 * DeleteDuplicates
 * @description 删除排序链表中的重复元素
 * @author answer_wx
 * @date 2022/7/3 23:39
 * @version 1.0
 */
public class DeleteDuplicates {
    /*public static ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        head.next = deleteDuplicates(head.next);
        if (head.val == head.next.val) {
            return head.next;
        }
        return head;
    }*/

    public static ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.val == cur.next.val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode node4 = new ListNode(3);
        ListNode node3 = new ListNode(3, node4);
        ListNode node2 = new ListNode(2, node3);
        ListNode node1 = new ListNode(1, node2);
        ListNode node0 = new ListNode(1, node1);
        ListNode node = deleteDuplicates(node0);
        while (node != null) {
            System.out.print(node.val);
            node = node.next;
        }
    }
}